package com.meichis.yndms.repository.manager;

import android.util.Log;

import androidx.collection.ArrayMap;
import androidx.lifecycle.MutableLiveData;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.yndms.http.CallSubscriber;
import com.meichis.yndms.http.IWebServiceCallback;
import com.meichis.yndms.http.RetrofitUtil.RxSchedulersHelper;
import com.meichis.yndms.model.Picture;
import com.meichis.yndms.model.WSIResultPack;
import com.meichis.yndms.repository.ApiUtil;

public class UploadManager extends BaseManager {

    private static UploadManager INSTANCE;

    public static UploadManager getInstance() {
        if (INSTANCE == null) {
            synchronized (UploadManager.class) {
                if (INSTANCE == null) {
                    INSTANCE = new UploadManager();
                }
            }
        }
        return INSTANCE;
    }


    public void UploadVisitWorkPicture(int sequence, Picture m, IWebServiceCallback callback) {
        //不为空标识从环节中提交的照片 否则就是从首页提交的照片  走不同的接口提交
        ArrayMap hs = new ArrayMap<>();
        hs.put("VisitWorkID", m.getRelateID());
        hs.put("ProcessCode", m.getProcessCode());
        hs.put("PicName", m.getFillName());
        hs.put("Description", m.getRemark());
        hs.put("PicData", Tools.filetoString(m.getFilePath()));
        hs.put("PicGUID", m.getGuid());

        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack(sequence, hs, "PBMService.UploadVisitWorkPicture"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<WSIResultPack>() {
                }.getType(), new IWebServiceCallback<WSIResultPack>() {
                    @Override
                    public void onSucceed(int sequence, WSIResultPack resultPack, String ReturnInfo, int Return) {
                        callback.onSucceed(sequence, resultPack, ReturnInfo, Return);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        callback.onFailure(sequence, errorCode, error);
                    }
                }));
    }

    public void UploadInspectPicture(int sequence, Picture m, IWebServiceCallback callback) {
        //不为空标识从环节中提交的照片 否则就是从首页提交的照片  走不同的接口提交
        ArrayMap hs = new ArrayMap<>();
        hs.put("ResultID", m.getRelateID());
        hs.put("PicName", m.getFillName());
        hs.put("PicData", Tools.filetoString(m.getFilePath()));
        hs.put("PicGUID", m.getGuid());
        hs.put("Description", m.getRemark());
        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack(sequence, hs, "PBMService.UploadInspectPicture"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<WSIResultPack>() {
                }.getType(), new IWebServiceCallback<String>() {
                    @Override
                    public void onSucceed(int sequence, String resultPack, String ReturnInfo, int Return) {
                        Log.e("xmy", new Gson().toJson(resultPack));
                        callback.onSucceed(sequence, resultPack, ReturnInfo, Return);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        callback.onFailure(sequence, errorCode, error);
                    }
                }));
    }

    public void UploadPicture(final MutableLiveData<Boolean> showDialog, int sequence, Picture m, IWebServiceCallback callback) {
        //终端反馈上传图片
        ArrayMap hs = new ArrayMap<>();
        hs.put("PicData", Tools.filetoString(m.getFilePath()));
        hs.put("RelateID", 1);
        hs.put("RelateType", m.getRelateType());
        hs.put("PicName", m.getFillName());
        hs.put("PicGUID", m.getGuid());
        showDialog.setValue(true);

        getRootApi().RequestSFA(getToken(), ApiUtil.getRequestPack(sequence, hs, "PublicService.UploadPicture"))
                .compose(RxSchedulersHelper.rxSchedulerHelper())
                .subscribe(new CallSubscriber(new TypeToken<WSIResultPack>() {
                }.getType(), new IWebServiceCallback<WSIResultPack>() {
                    @Override
                    public void onSucceed(int sequence, WSIResultPack resultPack, String ReturnInfo, int Return) {
                        showDialog.setValue(false);
                        callback.onSucceed(sequence, resultPack, ReturnInfo, Return);
                    }

                    @Override
                    public void onFailure(int sequence, int errorCode, String error) {
                        callback.onFailure(sequence, errorCode, error);
                        showDialog.setValue(false);
                    }
                }));
    }
}
